package soundwave.com.demo.list;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import soundwave.com.demo.R;
import soundwave.com.demo.activity.City_coupon;
import soundwave.com.demo.activity.Contact_platform;
import soundwave.com.demo.activity.Integral;
import soundwave.com.demo.activity.Login;
import soundwave.com.demo.activity.Modify_name;
import soundwave.com.demo.activity.Modify_password1;
import soundwave.com.demo.activity.Takeoutfood_coupon;
import soundwave.com.demo.activity.Takeoutfood_order;
import soundwave.com.demo.adpater.LoginList_adapert;
import soundwave.com.demo.entry.LoginSingle;
import soundwave.com.demo.test.LoginList_test;

/**
 * Created by xu on 2017/3/20.
 */

public class LoginList extends Activity {
    private List<LoginList_test> mData;
    private Context context = this;
    public LoginSingle loginSingle;
    private TextView back;
    private TextView login;
    private ListView listview;
    private LoginList_adapert adapert;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myinterface);
        initView();
        getmData();
        loginSingle = LoginSingle.getInstance();
        adapert = new LoginList_adapert(context, mData);
        listview.setAdapter(adapert);
        adapert.notifyDataSetChanged();
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String isLogin = login.getText().toString();
                if (isLogin.equals("登陆")) {
                    startActivity(new Intent(LoginList.this, Login.class));
                } else if (isLogin.equals("退出")) {
                    new AlertDialog.Builder(context)
                            .setTitle("提示")
                            .setMessage("您确定要退出登录吗？")
                            .setPositiveButton("取消", null)
                            .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    SharedPreferences sharedPreferences = getSharedPreferences("login", MODE_APPEND);
                                    SharedPreferences.Editor editor = sharedPreferences.edit();
                                    editor.putBoolean("isLogin", false);
                                    editor.putString("isName", "未登录");
                                    editor.commit();
                                    loginSingle.setLogin(false);
                                    loginSingle.setName("未登录");
                                    startActivity(new Intent(LoginList.this, Login.class));
                                }
                            }).show();

                }
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                switch (i) {
                    case 0:
                        if (loginSingle.isLogin()) {
                            startActivity(new Intent(LoginList.this, Modify_name.class));

                        } else {
                            startActivity(new Intent(LoginList.this, Login.class));


                        }
                        break;
                    case 1:
                        if (loginSingle.isLogin()) {
                            startActivity(new Intent(LoginList.this, MessageList.class));
                        } else {
                            startActivity(new Intent(LoginList.this, Login.class));
                        }
                        break;
                    case 2:
                        if (loginSingle.isLogin()) {
                            startActivity(new Intent(LoginList.this, Integral.class));
                        } else {
                            startActivity(new Intent(LoginList.this, Login.class));
                        }
                        break;
                    case 3:
                        if (loginSingle.isLogin()) {
                            startActivity(new Intent(LoginList.this, Takeoutfood_order.class));
                        } else {
                             startActivity(new Intent(LoginList.this,Login.class));
                        }
                        break;
                    case 4:
                        if (loginSingle.isLogin()){
                            startActivity(new Intent(LoginList.this, Takeoutfood_coupon.class));
                        }else {
                            startActivity(new Intent(LoginList.this,Login.class));
                        }
                        break;
                    case 5:
                        if (loginSingle.isLogin()){
                            startActivity(new Intent(LoginList.this,MyadressList.class));
                        }else {
                            startActivity(new Intent(LoginList.this,Login.class));
                        }
                        break;
                    case 6:
                        if (loginSingle.isLogin()){
                            startActivity(new Intent(LoginList.this, City_coupon.class));
                        }else {
                            startActivity(new Intent(LoginList.this,Login.class));
                        }
                        break;
                    case 7:
                         if (loginSingle.isLogin()){
                             startActivity(new Intent(LoginList.this, Modify_password1.class));
                         }else {
                             startActivity(new Intent(LoginList.this,Login.class));
                         }
                        break;
                    case 8:
                        startActivity(new Intent(LoginList.this, Contact_platform.class));

                }
            }
        });


    }

    private List<LoginList_test> getmData() {
        mData = new ArrayList<LoginList_test>();
        LoginList_test loginList_test1 = new LoginList_test();
        loginList_test1.setImg(R.mipmap.my);
        loginList_test1.setTitle("未登录");
        mData.add(loginList_test1);
        /*mData = new ArrayList<>();
        map = new HashMap<String, Object>();
        map.put("img", R.mipmap.my);
        map.put("title", "未登陆");
        mData.add(map);*/
        LoginList_test loginList_test2 = new LoginList_test();
        loginList_test2.setImg(R.mipmap.messageslist);
        loginList_test2.setTitle("消息列表");
        loginList_test2.setContent("点击查看收到的消息");
        mData.add(loginList_test2);


       /* map = new HashMap<String, Object>();
        map.put("img", R.mipmap.messageslist);
        map.put("title", "消息列表");
        map.put("text", "点击查看收到的消息");
        mData.add(map);*/
        LoginList_test loginList_test3 = new LoginList_test();
        loginList_test3.setImg(R.mipmap.integral);
        loginList_test3.setTitle("积分");
        loginList_test3.setContent("查看积分说明");
        mData.add(loginList_test3);
        /*map = new HashMap<String, Object>();
        map.put("img", R.mipmap.integral);
        map.put("title", "积分");
        map.put("text", "查看积分说明");
        mData.add(map);*/
        LoginList_test loginList_test4 = new LoginList_test();
        loginList_test4.setImg(R.mipmap.order);
        loginList_test4.setTitle("外卖订单");
        loginList_test4.setContent("查看我的订单");
        mData.add(loginList_test4);
       /* map = new HashMap<String, Object>();
        map.put("img", R.mipmap.order);
        map.put("title", "外卖订单");
        map.put("text", "查看我的订单");
        mData.add(map);*/
        LoginList_test loginList_test5 = new LoginList_test();
        loginList_test5.setImg(R.mipmap.notused);
        loginList_test5.setTitle("外卖优惠券");
        loginList_test5.setContent("点击查看");
        mData.add(loginList_test5);
        /*map = new HashMap<String, Object>();
        map.put("img", R.mipmap.notused);
        map.put("title", "外卖优惠券");
        map.put("text", "点击查看");
        mData.add(map);*/
        LoginList_test loginList_test6 = new LoginList_test();
        loginList_test6.setImg(R.mipmap.address);
        loginList_test6.setTitle("收货地址");
        loginList_test6.setContent("点击查看");
        mData.add(loginList_test6);
       /* map = new HashMap<String, Object>();
        map.put("img", R.mipmap.address);
        map.put("title", "收货地址");
        map.put("text", "点击查看");
        mData.add(map);*/
        LoginList_test loginList_test7 = new LoginList_test();
        loginList_test7.setImg(R.mipmap.coupon);
        loginList_test7.setTitle("同城商家优惠卷");
        mData.add(loginList_test7);
       /* map = new HashMap<String, Object>();
        map.put("img", R.mipmap.coupon);
        map.put("title", "同城商家优惠券");
        mData.add(map);*/
        LoginList_test loginList_test8 = new LoginList_test();
        loginList_test8.setImg(R.mipmap.wordmodification);
        loginList_test8.setTitle("修改登录密码");
        loginList_test8.setContent("修改密码");
        mData.add(loginList_test8);
        /*map = new HashMap<String, Object>();
        map.put("img", R.mipmap.wordmodification);
        map.put("title", "修改登陆密码");
        map.put("text", "修改密码");
        mData.add(map);*/
        LoginList_test loginList_test9 = new LoginList_test();
        loginList_test9.setImg(R.mipmap.wordmodification);
        loginList_test9.setTitle("联系晓事平台");
        mData.add(loginList_test9);
       /* map = new HashMap<String, Object>();
        map.put("img", R.mipmap.wordmodification);
        map.put("title", "联系晓事平台");
        mData.add(map);*/


        return mData;
    }

    private void initView() {
        back = (TextView) findViewById(R.id.back);
        login = (TextView) findViewById(R.id.login);
        listview = (ListView) findViewById(R.id.listview);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mData.remove(0);
        if (loginSingle.isLogin()) {
            String name = loginSingle.getName();
            LoginList_test loginList_test1 = new LoginList_test();
            loginList_test1.setImg(R.mipmap.my);
            loginList_test1.setTitle(name);
            mData.add(0, loginList_test1);
            login.setText("退出");

        } else {
            LoginList_test loginList_test1 = new LoginList_test();
            loginList_test1.setImg(R.mipmap.my);
            loginList_test1.setTitle("未登录");
            mData.add(0, loginList_test1);
            login.setText("登陆");
        }
        adapert.notifyDataSetChanged();


    }

}
